CREATE TABLE ACAO (CD_ACAO INT, CD_PROPOSICAO INT NOT NULL, NAME VARCHAR2(255) NOT NULL, DE_ACAO VARCHAR2(4000) NOT NULL, DT_LIMITE_CONCLUSAO DATE NOT NULL, DT_CONCLUSAO DATE, DELETED NUMBER(1) DEFAULT '0' NOT NULL, CREATED_AT DATE, UPDATED_AT DATE, SLUG VARCHAR2(255), PRIMARY KEY(CD_ACAO))
/
CREATE TABLE CAMARA (CD_CAMARA INT, CD_TIPO_CAMARA INT NOT NULL, CD_USR_SECRETARIO INT NOT NULL, NO_LONGO VARCHAR2(255) NOT NULL, NAME VARCHAR2(50) NOT NULL UNIQUE, SG_CAMARA VARCHAR2(10) NOT NULL UNIQUE, DELETED NUMBER(1) DEFAULT '0' NOT NULL, CREATED_AT DATE, UPDATED_AT DATE, SLUG VARCHAR2(255), PRIMARY KEY(CD_CAMARA))
/
CREATE TABLE CIDADE (CD_CIDADE INT, CD_UF INT NOT NULL, NAME VARCHAR2(255) NOT NULL, CREATED_AT DATE, UPDATED_AT DATE, PRIMARY KEY(CD_CIDADE))
/
CREATE TABLE EVENTO (CD_EVENTO INT, CD_TIPO_EVENTO INT NOT NULL, CD_CAMARA INT NOT NULL, CD_CIDADE INT NOT NULL, NR_EVENTO INT, DT_HR_INICIO DATE NOT NULL, DT_HR_TERMINO DATE NOT NULL, DE_ENDERECO VARCHAR2(255) NOT NULL, IC_CONFIRMADO NUMBER(1) DEFAULT '1' NOT NULL, PAUTA VARCHAR2(255), PAUTA_ENVIADA_EM DATE, ATA VARCHAR2(255), ATA_ENVIADA_EM DATE, DELETED NUMBER(1) DEFAULT '0' NOT NULL, CREATED_AT DATE, UPDATED_AT DATE, PRIMARY KEY(CD_EVENTO))
/
CREATE TABLE ORGAO_EXTERNO (CD_ORGAO_EXTERNO INT, NO_LONGO VARCHAR2(255) NOT NULL, NAME VARCHAR2(50) NOT NULL UNIQUE, SG_ORGAO_EXTERNO VARCHAR2(10) NOT NULL UNIQUE, CREATED_AT DATE, UPDATED_AT DATE, DELETED NUMBER(1) DEFAULT '0' NOT NULL, PRIMARY KEY(CD_ORGAO_EXTERNO))
/
CREATE TABLE PROPOSICAO (CD_PROPOSICAO INT, CD_CAMARA INT NOT NULL, CD_TEMA INT, NAME VARCHAR2(255) NOT NULL, DE_PROPOSICAO VARCHAR2(4000) NOT NULL, DT_LIMITE_CONCLUSAO DATE NOT NULL, DELETED NUMBER(1) DEFAULT '0' NOT NULL, CREATED_AT DATE, UPDATED_AT DATE, SLUG VARCHAR2(255), PRIMARY KEY(CD_PROPOSICAO))
/
CREATE TABLE PROVIDENCIA_ACAO (CD_PROVIDENCIA_ACAO INT, CD_ACAO INT NOT NULL, CD_USUARIO INT NOT NULL, DE_PROVIDENCIA VARCHAR2(4000) NOT NULL, DELETED NUMBER(1) DEFAULT '0' NOT NULL, CREATED_AT DATE, UPDATED_AT DATE, PRIMARY KEY(CD_PROVIDENCIA_ACAO))
/
CREATE TABLE TEMA (CD_TEMA INT, NAME VARCHAR2(50) NOT NULL, CD_TEMA_PAI INT, CREATED_AT DATE, UPDATED_AT DATE, DELETED NUMBER(1) DEFAULT '0' NOT NULL, PRIMARY KEY(CD_TEMA))
/
CREATE TABLE TIPO_CAMARA (CD_TIPO_CAMARA INT, NAME VARCHAR2(50) NOT NULL UNIQUE, PRIMARY KEY(CD_TIPO_CAMARA))
/
CREATE TABLE TIPO_EVENTO (CD_TIPO_EVENTO INT, NAME VARCHAR2(50) NOT NULL UNIQUE, PRIMARY KEY(CD_TIPO_EVENTO))
/
CREATE TABLE TIPO_USUARIO (CD_TIPO_USUARIO INT, NAME VARCHAR2(50) NOT NULL UNIQUE, PRIMARY KEY(CD_TIPO_USUARIO))
/
CREATE TABLE UF (CD_UF INT, NAME VARCHAR2(2) NOT NULL UNIQUE, PRIMARY KEY(CD_UF))
/
CREATE TABLE USR_EVENTO (CD_USUARIO INT, CD_EVENTO INT, CREATED_AT DATE, UPDATED_AT DATE, PRIMARY KEY(CD_USUARIO, CD_EVENTO))
/
CREATE TABLE USR_ORG_EXTERNO (CD_USUARIO INT, CD_ORGAO_EXTERNO INT, PRIMARY KEY(CD_USUARIO, CD_ORGAO_EXTERNO))
/
CREATE TABLE USUARIO (CD_USUARIO INT, CD_TIPO_USUARIO INT NOT NULL, NAME VARCHAR2(255) NOT NULL, NO_EMAIL VARCHAR2(255) NOT NULL UNIQUE, TELEFONE INT, CO_SENHA VARCHAR2(20), DT_ULTIMO_ACESSO DATE, RESET_SENHA NUMBER(1) DEFAULT '1' NOT NULL, DELETED NUMBER(1) DEFAULT '0' NOT NULL, CREATED_AT DATE, UPDATED_AT DATE, PRIMARY KEY(CD_USUARIO))
/
CREATE SEQUENCE ACAO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE CAMARA_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE CIDADE_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE EVENTO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE ORGAO_EXTERNO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE PROPOSICAO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE PROVIDENCIA_ACAO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE TEMA_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE TIPO_CAMARA_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE TIPO_EVENTO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE TIPO_USUARIO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE UF_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE SEQUENCE USUARIO_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'ACAO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE ACAO ADD CONSTRAINT ACAO_AI_PK_IDX PRIMARY KEY (CD_ACAO)';
  END IF;
END;
/
ALTER TABLE ACAO ADD FOREIGN KEY (CD_PROPOSICAO) REFERENCES PROPOSICAO(CD_PROPOSICAO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'CAMARA' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE CAMARA ADD CONSTRAINT CAMARA_AI_PK_IDX PRIMARY KEY (CD_CAMARA)';
  END IF;
END;
/
ALTER TABLE CAMARA ADD FOREIGN KEY (CD_USR_SECRETARIO) REFERENCES USUARIO(CD_USUARIO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE CAMARA ADD FOREIGN KEY (CD_TIPO_CAMARA) REFERENCES TIPO_CAMARA(CD_TIPO_CAMARA) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'CIDADE' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE CIDADE ADD CONSTRAINT CIDADE_AI_PK_IDX PRIMARY KEY (CD_CIDADE)';
  END IF;
END;
/
ALTER TABLE CIDADE ADD FOREIGN KEY (CD_UF) REFERENCES UF(CD_UF) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'EVENTO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE EVENTO ADD CONSTRAINT EVENTO_AI_PK_IDX PRIMARY KEY (CD_EVENTO)';
  END IF;
END;
/
ALTER TABLE EVENTO ADD FOREIGN KEY (CD_TIPO_EVENTO) REFERENCES TIPO_EVENTO(CD_TIPO_EVENTO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE EVENTO ADD FOREIGN KEY (CD_CIDADE) REFERENCES CIDADE(CD_CIDADE) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE EVENTO ADD FOREIGN KEY (CD_CAMARA) REFERENCES CAMARA(CD_CAMARA) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'ORGAO_EXTERNO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE ORGAO_EXTERNO ADD CONSTRAINT ORGAO_EXTERNO_AI_PK_IDX PRIMARY KEY (CD_ORGAO_EXTERNO)';
  END IF;
END;
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'PROPOSICAO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE PROPOSICAO ADD CONSTRAINT PROPOSICAO_AI_PK_IDX PRIMARY KEY (CD_PROPOSICAO)';
  END IF;
END;
/
ALTER TABLE PROPOSICAO ADD FOREIGN KEY (CD_TEMA) REFERENCES TEMA(CD_TEMA) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE PROPOSICAO ADD FOREIGN KEY (CD_CAMARA) REFERENCES CAMARA(CD_CAMARA) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'PROVIDENCIA_ACAO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE PROVIDENCIA_ACAO ADD CONSTRAINT PROVIDENCIA_ACAO_AI_PK_IDX PRIMARY KEY (CD_PROVIDENCIA_ACAO)';
  END IF;
END;
/
ALTER TABLE PROVIDENCIA_ACAO ADD FOREIGN KEY (CD_USUARIO) REFERENCES USUARIO(CD_USUARIO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE PROVIDENCIA_ACAO ADD FOREIGN KEY (CD_ACAO) REFERENCES ACAO(CD_ACAO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'TEMA' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE TEMA ADD CONSTRAINT TEMA_AI_PK_IDX PRIMARY KEY (CD_TEMA)';
  END IF;
END;
/
ALTER TABLE TEMA ADD FOREIGN KEY (CD_TEMA_PAI) REFERENCES TEMA(CD_TEMA) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'TIPO_CAMARA' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE TIPO_CAMARA ADD CONSTRAINT TIPO_CAMARA_AI_PK_IDX PRIMARY KEY (CD_TIPO_CAMARA)';
  END IF;
END;
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'TIPO_EVENTO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE TIPO_EVENTO ADD CONSTRAINT TIPO_EVENTO_AI_PK_IDX PRIMARY KEY (CD_TIPO_EVENTO)';
  END IF;
END;
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'TIPO_USUARIO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE TIPO_USUARIO ADD CONSTRAINT TIPO_USUARIO_AI_PK_IDX PRIMARY KEY (CD_TIPO_USUARIO)';
  END IF;
END;
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'UF' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE UF ADD CONSTRAINT UF_AI_PK_IDX PRIMARY KEY (CD_UF)';
  END IF;
END;
/
ALTER TABLE USR_EVENTO ADD FOREIGN KEY (CD_USUARIO) REFERENCES USUARIO(CD_USUARIO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE USR_EVENTO ADD FOREIGN KEY (CD_EVENTO) REFERENCES EVENTO(CD_EVENTO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE USR_ORG_EXTERNO ADD FOREIGN KEY (CD_USUARIO) REFERENCES USUARIO(CD_USUARIO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
ALTER TABLE USR_ORG_EXTERNO ADD FOREIGN KEY (CD_ORGAO_EXTERNO) REFERENCES ORGAO_EXTERNO(CD_ORGAO_EXTERNO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
DECLARE
  CONSTRAINTS_COUNT NUMBER;
BEGIN
  SELECT COUNT(CONSTRAINT_NAME) INTO CONSTRAINTS_COUNT FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'USUARIO' AND CONSTRAINT_TYPE = 'P';
  IF CONSTRAINTS_COUNT = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE USUARIO ADD CONSTRAINT USUARIO_AI_PK_IDX PRIMARY KEY (CD_USUARIO)';
  END IF;
END;
/
ALTER TABLE USUARIO ADD FOREIGN KEY (CD_TIPO_USUARIO) REFERENCES TIPO_USUARIO(CD_TIPO_USUARIO) NOT DEFERRABLE INITIALLY IMMEDIATE
/
CREATE TRIGGER ACAO_AI_PK
   BEFORE INSERT
   ON ACAO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT ACAO_SEQ.NEXTVAL INTO :NEW.CD_ACAO FROM DUAL;
   IF (:NEW.CD_ACAO IS NULL OR :NEW.CD_ACAO = 0) THEN
      SELECT ACAO_SEQ.NEXTVAL INTO :NEW.CD_ACAO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('ACAO_SEQ');
      SELECT :NEW.CD_ACAO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT ACAO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER CAMARA_AI_PK
   BEFORE INSERT
   ON CAMARA
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT CAMARA_SEQ.NEXTVAL INTO :NEW.CD_CAMARA FROM DUAL;
   IF (:NEW.CD_CAMARA IS NULL OR :NEW.CD_CAMARA = 0) THEN
      SELECT CAMARA_SEQ.NEXTVAL INTO :NEW.CD_CAMARA FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('CAMARA_SEQ');
      SELECT :NEW.CD_CAMARA INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT CAMARA_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER CIDADE_AI_PK
   BEFORE INSERT
   ON CIDADE
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT CIDADE_SEQ.NEXTVAL INTO :NEW.CD_CIDADE FROM DUAL;
   IF (:NEW.CD_CIDADE IS NULL OR :NEW.CD_CIDADE = 0) THEN
      SELECT CIDADE_SEQ.NEXTVAL INTO :NEW.CD_CIDADE FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('CIDADE_SEQ');
      SELECT :NEW.CD_CIDADE INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT CIDADE_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER EVENTO_AI_PK
   BEFORE INSERT
   ON EVENTO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT EVENTO_SEQ.NEXTVAL INTO :NEW.CD_EVENTO FROM DUAL;
   IF (:NEW.CD_EVENTO IS NULL OR :NEW.CD_EVENTO = 0) THEN
      SELECT EVENTO_SEQ.NEXTVAL INTO :NEW.CD_EVENTO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('EVENTO_SEQ');
      SELECT :NEW.CD_EVENTO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT EVENTO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER ORGAO_EXTERNO_AI_PK
   BEFORE INSERT
   ON ORGAO_EXTERNO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT ORGAO_EXTERNO_SEQ.NEXTVAL INTO :NEW.CD_ORGAO_EXTERNO FROM DUAL;
   IF (:NEW.CD_ORGAO_EXTERNO IS NULL OR :NEW.CD_ORGAO_EXTERNO = 0) THEN
      SELECT ORGAO_EXTERNO_SEQ.NEXTVAL INTO :NEW.CD_ORGAO_EXTERNO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('ORGAO_EXTERNO_SEQ');
      SELECT :NEW.CD_ORGAO_EXTERNO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT ORGAO_EXTERNO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER PROPOSICAO_AI_PK
   BEFORE INSERT
   ON PROPOSICAO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT PROPOSICAO_SEQ.NEXTVAL INTO :NEW.CD_PROPOSICAO FROM DUAL;
   IF (:NEW.CD_PROPOSICAO IS NULL OR :NEW.CD_PROPOSICAO = 0) THEN
      SELECT PROPOSICAO_SEQ.NEXTVAL INTO :NEW.CD_PROPOSICAO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('PROPOSICAO_SEQ');
      SELECT :NEW.CD_PROPOSICAO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT PROPOSICAO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER PROVIDENCIA_ACAO_AI_PK
   BEFORE INSERT
   ON PROVIDENCIA_ACAO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT PROVIDENCIA_ACAO_SEQ.NEXTVAL INTO :NEW.CD_PROVIDENCIA_ACAO FROM DUAL;
   IF (:NEW.CD_PROVIDENCIA_ACAO IS NULL OR :NEW.CD_PROVIDENCIA_ACAO = 0) THEN
      SELECT PROVIDENCIA_ACAO_SEQ.NEXTVAL INTO :NEW.CD_PROVIDENCIA_ACAO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('PROVIDENCIA_ACAO_SEQ');
      SELECT :NEW.CD_PROVIDENCIA_ACAO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT PROVIDENCIA_ACAO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER TEMA_AI_PK
   BEFORE INSERT
   ON TEMA
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT TEMA_SEQ.NEXTVAL INTO :NEW.CD_TEMA FROM DUAL;
   IF (:NEW.CD_TEMA IS NULL OR :NEW.CD_TEMA = 0) THEN
      SELECT TEMA_SEQ.NEXTVAL INTO :NEW.CD_TEMA FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('TEMA_SEQ');
      SELECT :NEW.CD_TEMA INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT TEMA_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER TIPO_CAMARA_AI_PK
   BEFORE INSERT
   ON TIPO_CAMARA
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT TIPO_CAMARA_SEQ.NEXTVAL INTO :NEW.CD_TIPO_CAMARA FROM DUAL;
   IF (:NEW.CD_TIPO_CAMARA IS NULL OR :NEW.CD_TIPO_CAMARA = 0) THEN
      SELECT TIPO_CAMARA_SEQ.NEXTVAL INTO :NEW.CD_TIPO_CAMARA FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('TIPO_CAMARA_SEQ');
      SELECT :NEW.CD_TIPO_CAMARA INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT TIPO_CAMARA_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER TIPO_EVENTO_AI_PK
   BEFORE INSERT
   ON TIPO_EVENTO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT TIPO_EVENTO_SEQ.NEXTVAL INTO :NEW.CD_TIPO_EVENTO FROM DUAL;
   IF (:NEW.CD_TIPO_EVENTO IS NULL OR :NEW.CD_TIPO_EVENTO = 0) THEN
      SELECT TIPO_EVENTO_SEQ.NEXTVAL INTO :NEW.CD_TIPO_EVENTO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('TIPO_EVENTO_SEQ');
      SELECT :NEW.CD_TIPO_EVENTO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT TIPO_EVENTO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER TIPO_USUARIO_AI_PK
   BEFORE INSERT
   ON TIPO_USUARIO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT TIPO_USUARIO_SEQ.NEXTVAL INTO :NEW.CD_TIPO_USUARIO FROM DUAL;
   IF (:NEW.CD_TIPO_USUARIO IS NULL OR :NEW.CD_TIPO_USUARIO = 0) THEN
      SELECT TIPO_USUARIO_SEQ.NEXTVAL INTO :NEW.CD_TIPO_USUARIO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('TIPO_USUARIO_SEQ');
      SELECT :NEW.CD_TIPO_USUARIO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT TIPO_USUARIO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER UF_AI_PK
   BEFORE INSERT
   ON UF
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT UF_SEQ.NEXTVAL INTO :NEW.CD_UF FROM DUAL;
   IF (:NEW.CD_UF IS NULL OR :NEW.CD_UF = 0) THEN
      SELECT UF_SEQ.NEXTVAL INTO :NEW.CD_UF FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('UF_SEQ');
      SELECT :NEW.CD_UF INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT UF_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
CREATE TRIGGER USUARIO_AI_PK
   BEFORE INSERT
   ON USUARIO
   FOR EACH ROW
DECLARE
   LAST_SEQUENCE NUMBER;
   LAST_INSERTID NUMBER;
BEGIN
   SELECT USUARIO_SEQ.NEXTVAL INTO :NEW.CD_USUARIO FROM DUAL;
   IF (:NEW.CD_USUARIO IS NULL OR :NEW.CD_USUARIO = 0) THEN
      SELECT USUARIO_SEQ.NEXTVAL INTO :NEW.CD_USUARIO FROM DUAL;
   ELSE
      SELECT NVL(LAST_NUMBER, 0) INTO LAST_SEQUENCE
        FROM USER_SEQUENCES
       WHERE UPPER(SEQUENCE_NAME) = UPPER('USUARIO_SEQ');
      SELECT :NEW.CD_USUARIO INTO LAST_INSERTID FROM DUAL;
      WHILE (LAST_INSERTID > LAST_SEQUENCE) LOOP
         SELECT USUARIO_SEQ.NEXTVAL INTO LAST_SEQUENCE FROM DUAL;
      END LOOP;
   END IF;
END;
/
